<?php

namespace app\api\controller;

use app\admin\model\LpdhCategory;
use app\common\controller\Api;

/**
 * 礼品兑换接口
 */
class Lpdh extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $lpdhModel = null;
    protected $lpdhCategoryModel = null;
    protected $lpxxModel=null;

    public function _initialize()
    {
        parent::_initialize();
        $this->lpdhModel = new \app\admin\model\Lpdh();
        $this->lpdhCategoryModel =new LpdhCategory();
        $this->lpxxModel =new \app\admin\model\Lpxx();
		$this->userModel = new \app\admin\model\User();
    }

    /**
     * 礼品兑换分类列表
     *
     */
    public function getlpdhCategoryList()
    {
        $lpdhCategory = $this->lpdhCategoryModel
            ->select();
        $this->outputData($lpdhCategory);
    }
    /**
     * 礼品兑换列表
     */
    public function getlpdhList()
    {
        $page = $this->request->get('page') - 1;
        $page=$page>0?$page:0;
        $limit = $this->request->get('pageSize');
        $limit=$limit>0?$limit:5;
        $start = $page * $limit;
        $status=$this->request->get('status');
        $catid=$this->request->get('catid');
        $groupId=$this->request->get('group_id');
        $uid=$this->request->get('uid');
		$where=[];
        //按状态
        if($status){
            $where['status']=$status;
        }
        //按用户组
        if(isset($groupId)){
            $where['groupid']=$groupId;
        }
		//按用户
        if(isset($uid)){
            $where['uid']=$uid;
        }
        $lpdhList = $this->lpdhModel
            ->limit($start,$limit)
            ->where($where)
            ->select();
        $this->outputData($lpdhList);
    }
    /**
     * 礼品兑换详情
     */
    public function getlpdhDetail()
    {
        $id=$this->request->request('id');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
        $lpdhList = $this->lpdhModel
            ->alias('f')
            ->where('f.id',$id)
            ->find();
        $this->outputData($lpdhList);
    }
    /**
     * 礼品兑换添加
     */
    public function getLpdhAdd()
    {
        $uid=$this->request->request('uid');
        $mobile=$this->request->request('mobile');
        $address=$this->request->request('address');
        $name=$this->request->request('name');
        $lpId=$this->request->request('lpId');
        $groupId=$this->request->request('groupid');
        if(!$uid||!$mobile||!$address||!$name||!$lpId||$groupId){
            $this->outputError('请填写完整的信息');
        }
        //礼品信息
        $lpxx=$this->lpxxModel->where('id',$lpId)->find();
        if(!$lpxx){
            $this->outputError('礼品不存在');
        }
        $data=['uid'=>$uid,'inputtime'=>time(),'mobile'=>$mobile,'address'=>$address,'name'=>$name,'lp_name'=>$lpxx['title'],'points'=>$lpxx['jifen']];
        $re = $this->lpdhModel
            ->insert($data);
        if($re){
            $this->outputData('添加成功');
        }else{
            $this->outputData('添加失败');
        }
    }
    /**
     * 礼品兑换审核
     */
    public function getLpdhUpdate()
    {
        $uid=$this->request->request('uid');
        $id=$this->request->request('id');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
		//积分
        $res=$this->lpdhModel->where('id',$id)->find();
        $cuid=$res['uid'];
        $user=$this->userModel->where('id',$cuid)->find();
        if($user['score']<$res['points']){
           $this->outputError('积分不够');
        }else{
            $points=$user['score']-$res['points'];
        }
        $data=['score'=>$points];
        $re = $this->lpdhModel
            ->where('id',$id)
            ->update(['operator'=>$uid,'status'=>1]);
        if($re){
            $user=$this->userModel
                ->where('id', $cuid)
                ->update($data);
        } 
        if($re){
            $this->outputData('审核成功');
        }else{
            $this->outputData('审核失败');
        }
    }
}
